/**
 * This file contains javascript to handle information of chronic disease.
 */
////////////////// LOADING A PAGE ///////////////////
function loadPage(sourceDiv, actionURL, noteId) {
	if(noteId == null){
		noteId = '';
	}
    $.ajax ({
    	type:		"GET",
        url:        actionURL + "?set_ajax_view", 
        data:		"note_id=" + noteId,	             
        dataType:   "html",
        beforeSend: function() {                    
            $('div#noteListContent').hide(); 
            $('div#pageContent').hide("slow");                
        },
        complete:   function() {
            $('div#' + sourceDiv).show("fast");         
        },
        success:  function(data) {
            $("div#" + sourceDiv).html(data);          
        }
    });
}

///////////////// ADD-EDIT A NOTE ///////////////////
function editPrescNote(noteId){
	loadPage('pageContent', 'EditPrescriptionNotes', noteId);
}

function savePrescriptionNote(){
	var noteId	=	$('#note_id').val();
	var note	=	$('#note').val();
	
	if(!isNotEmpty($('#note'),_('Empty name'),'status_message')) {
		return;
	}
	$.ajax({
		type	:	'POST',
		url		:	'SavePrescriptionNoteAjax?set_ajax_view',
		data	:	{'pnote_id' : noteId, 'note' : note},
		beforeSend: function(){
			$('div#btn_save').hide();
			$('div#barLoading').show();
		},
		complete: function(){
			$('div#barLoading').hide();
			$('div#btn_save').show();
		},
		success	: function(data){
			$('div#pageContent').hide('slow');
			loadPage('noteListContent','ListPrescriptionNotes');
			$('div#noteListContent').show();
		}
	})
}

///////////////// CHANGE A NOTE'S STATUS ///////////////////
function changePrescNoteStatus(id, status){
	var message = _('Are you sure to delete this prescription note?');
	if(status == 'unlock'){
		message = _('Reuse this prescription note?');
	}
	promptConfirm(_("Delete"), message, function() {
		submitChangeStatus(id, status);  
		return true;
	},'OkCancel','warn',_('Cancel'));
}

function submitChangeStatus(id, status) {
	$.ajax ({
		url:	"ChangePNoteStatusAjax?set_ajax_view",
		data:	{'id' : id, 'status' : status},
		success: function(data) {
			var noteTable = $('table#note_list');
			var html = '';
			var changedStatus = '';
			if(status == 'lock'){
				changedStatus = '<span class="impression">'+ _("Temporary delete")+ '</span>';
				html = '<a class="btn" href="javascript:changePrescNoteStatus(' + id + ',\'unlock\');" title="'+ _("Reuse this note")+ '">'
                	+ '<img src="../images/unlock_small.png" alt="'+ _("Unlock")+ '"></img></a>';
			}
			else if(status == 'unlock'){
				changedStatus = '<span>'+ _("In Use")+ '</span>';
				html = '<a class="btn" href="javascript:changePrescNoteStatus(' + id + ',\'lock\');" title="'+ _("Delete this note")+ '">'
                	+ '<img src="../images/lock_small.png" alt="'+ _("Temporary delete")+ '"></img></a>';
			}
			$('tr#' + id + ' td:nth-child(2)' + noteTable).html(changedStatus);
			$('tr#' + id + ' td:nth-child(3)' + noteTable).html(html);
		}
	});
}